﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AssignDataFromTC.ADTCExtensions.ADTCExtension
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Media;


namespace DnProcessSimulateCommands.AssignDataFromTC.ADTCExtensions;

public static class ADTCExtension
{
  public static bool ContainsIgnoreCase(this string str, string searchText)
  {
    string[] strArray = searchText.Split('*');
    if (strArray.Length > 2 || strArray.Length < 2)
      return str != null && str.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0;
    if (strArray[0].Length == 0)
      return strArray[1].Length == 0 || str.EndsWith(strArray[1], StringComparison.OrdinalIgnoreCase);
    if (strArray[1].Length == 0)
      return str.IndexOf(strArray[0], StringComparison.OrdinalIgnoreCase) == 0;
    return str.IndexOf(strArray[0], StringComparison.OrdinalIgnoreCase) == 0 && str.EndsWith(strArray[1], StringComparison.OrdinalIgnoreCase);
  }

  public static IEnumerable<T> Add<T>(this IEnumerable<T> list, T value)
  {
    foreach (T obj in list)
      yield return obj;
    yield return value;
  }

  public static void AddList<T>(this IEnumerable<T> list, List<T> values)
  {
    foreach (T obj in values)
      list.Add<T>(obj);
  }

  public static void RemoveFromIndex<T>(this Collection<T> source, int index)
  {
    for (int index1 = source.Count - 1; index1 >= 0 && index1 > index; --index1)
      source.RemoveAt(index1);
  }

  public static IEnumerable<T> GetVTDescendants<T>(DependencyObject depObj) where T : DependencyObject
  {
    if (depObj != null)
    {
      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); ++i)
      {
        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
        if (child != null && child is T)
          yield return child as T;
        foreach (T vtDescendant in ADTCExtension.GetVTDescendants<T>(child))
          yield return vtDescendant;
        child = (DependencyObject) null;
      }
    }
  }

  public static IEnumerable<T> GetLTDescendants<T>(DependencyObject depObj) where T : DependencyObject
  {
    if (depObj != null)
    {
      foreach (object child in LogicalTreeHelper.GetChildren(depObj))
      {
        if (child != null && child is T)
          yield return child as T;
        foreach (T ltDescendant in ADTCExtension.GetLTDescendants<T>(child as DependencyObject))
          yield return ltDescendant;
      }
    }
  }

  public static T GetVTDescendant<T>(DependencyObject depObj) where T : DependencyObject
  {
    IEnumerable<T> vtDescendants = ADTCExtension.GetVTDescendants<T>(depObj);
    return vtDescendants == null ? default (T) : vtDescendants.FirstOrDefault<T>();
  }

  public static T GetLTDescendant<T>(DependencyObject depObj) where T : DependencyObject
  {
    IEnumerable<T> ltDescendants = ADTCExtension.GetLTDescendants<T>(depObj);
    return ltDescendants == null ? default (T) : ltDescendants.FirstOrDefault<T>();
  }

  public static T GetLTAncestor<T>(object obj) where T : class
  {
    T ltAncestor = default (T);
    object current = (object) (obj as DependencyObject);
    while ((object) ltAncestor == null && current is DependencyObject)
    {
      current = (object) LogicalTreeHelper.GetParent(current as DependencyObject);
      if (current != null && current is T)
        ltAncestor = current as T;
    }
    return ltAncestor;
  }

  public static T GetVTAncestor<T>(object obj) where T : class
  {
    T vtAncestor = default (T);
    object reference = (object) (obj as DependencyObject);
    while ((object) vtAncestor == null && reference is DependencyObject)
    {
      reference = (object) VisualTreeHelper.GetParent(reference as DependencyObject);
      if (reference != null && reference is T)
        vtAncestor = reference as T;
    }
    return vtAncestor;
  }
}
